//
//  YMCollectionViewController.swift
//  swift_review
//
//  Created by Andrew on 2024/11/24.
//

import UIKit

class YMCollectionNorVC: UIViewController {

    var collectionView: UICollectionView!
    var headerView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupHeaderView()
        setupCollectionView()
    }
    
    func setupHeaderView() {
        // 创建类似 TableView 的 HeaderView
        headerView = UIView()
        headerView.backgroundColor = .systemBlue
        
        let headerLabel = UILabel()
        headerLabel.text = "TableView-like Header View"
        headerLabel.textColor = .white
        headerLabel.textAlignment = .center
        headerLabel.font = UIFont.boldSystemFont(ofSize: 18)
        headerLabel.translatesAutoresizingMaskIntoConstraints = false
        
        headerView.translatesAutoresizingMaskIntoConstraints = false
        headerView.addSubview(headerLabel)
        view.addSubview(headerView)
        
        // 添加约束
        NSLayoutConstraint.activate([
            // HeaderView 顶部、左、右固定，设置高度
            headerView.topAnchor.constraint(equalTo: view.topAnchor),
            headerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            headerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            headerView.heightAnchor.constraint(equalToConstant: 150),
            
            // HeaderLabel 居中
            headerLabel.centerXAnchor.constraint(equalTo: headerView.centerXAnchor),
            headerLabel.centerYAnchor.constraint(equalTo: headerView.centerYAnchor)
        ])
    }
    
    func setupCollectionView() {
        // 创建 CollectionView 的布局
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: view.bounds.width / 2 - 15, height: 100) // 设置每个 item 的大小
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) // 设置每个 section 的边距
        layout.headerReferenceSize = CGSize(width: view.bounds.width, height: 50) // 设置 section header 的大小
        
        // 初始化 CollectionView
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .white
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        
        // 注册 Cell 和 Header
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
        
        // 添加到视图
        view.addSubview(collectionView)
        
        // 设置约束
        NSLayoutConstraint.activate([
            collectionView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
            collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }
}

extension YMCollectionNorVC: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2 // 两个 section
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10 // 每个 section 有 10 个 item
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.backgroundColor = indexPath.section == 0 ? .systemPink : .systemGreen
        return cell
    }
    
    // 配置 section 的 header
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader {
            let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath)
            header.backgroundColor = .lightGray
            
            // 添加标题
            for subview in header.subviews {
                subview.removeFromSuperview() // 清理重用的内容
            }
            let label = UILabel()
            label.text = "Section \(indexPath.section + 1)"
            label.textColor = .black
            label.textAlignment = .center
            label.font = UIFont.boldSystemFont(ofSize: 16)
            label.translatesAutoresizingMaskIntoConstraints = false
            header.addSubview(label)
            
            // 设置 label 的约束
            NSLayoutConstraint.activate([
                label.centerXAnchor.constraint(equalTo: header.centerXAnchor),
                label.centerYAnchor.constraint(equalTo: header.centerYAnchor)
            ])
            
            return header
        }
        return UICollectionReusableView()
    }
}
